


# ===参数设定
# select_stock_num = 10  # 选股数量
# c_rate = 1.5 / 10000  # 手续费
# t_rate = 1 / 1000  # 印花税


# ===导入数据
# 从hdf文件中读取整理好的所有股票数据
# file_path='/Users/zhenghuihuang/Desktop/研究生课程/面向金融的python/大作业/xbx_stock_2019_完整代码/data/basic-trading-data/h5_data/all_stock_data_M.h5'
# file_path='/Users/zhenghuihuang/Desktop/研究生课程/面向金融的python/大作业/xbx_stock_2019_完整代码/data/basic-trading-data/h5_data/all_stock_data_d_py3_w8.h5'
# file_path='/Users/zhenghuihuang/Desktop/研究生课程/面向金融的python/大作业/xbx_stock_2019_完整代码/data/basic-trading-data/h5_data/all_stock_data_d_py3_w8_1128.h5'
# file_path='/Users/zhenghuihuang/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/3c9f99e3d7e6e5c15c5a20322690de49/Message/MessageTemp/af3e19bb79c69fdc46aeebfa70a79aa9/File/all_stock_data_d_py3_w8.h5'
# key = 'df'
# file_path='/Users/zhenghuihuang/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/3c9f99e3d7e6e5c15c5a20322690de49/Message/MessageTemp/af3e19bb79c69fdc46aeebfa70a79aa9/File/hdfall_stock_data_d_py3_w8.h5'
# df = pd.read_hdf(file_path, 'df')
# df.dropna(subset=['下周期每天涨跌幅'], inplace=True)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from Functions import *

import pickle
import pandas as pd
pd.set_option('expand_frame_repr', False)  # 当列太多时显示完整

pd.set_option('expand_frame_repr', False)  # 当列太多时不换行
pd.set_option('display.max_rows', 5000)  # 最多显示数据的行数


class ImportData():

    def __init__(self, file_path, key):
        self.df=pd.read_hdf(file_path, key)
        self.df.dropna(subset=['下周期每天涨跌幅'], inplace=True)
        self.select_stock_num = 10  # 选股数量
        self.df = self.df[self.df['下日_是否交易'] == 1]
        self.df = self.df[self.df['下日_开盘涨停'] == False]
        self.df = self.df[self.df['下日_是否ST'] == False]
        self.df = self.df[self.df['下日_是否退市'] == False]
        self.ic_ir_dic={}

        # self.drop_list=[]

    def reset_select_stock_num(self, select_stock_num):
        self.select_stock_num = select_stock_num  # 选股数量


    def select_stock_single(self,factor):
        self.df[f"{factor}排名"] = self.df.groupby('日期')[factor].rank(method='first')
        self.df = self.df[self.df[f"{factor}排名"]<=self.select_stock_num]

    def select_multistock_normal(self,factor_list):
        all_alpha_list=self.df.columns.values.tolist()[self.df.columns.values.tolist().index('alpha001'):self.df.columns.values.tolist().index('交易天数')]
        drop_list=[]
        retain_alpha_list=[]
        # print(all_alpha_list)
        # print(factor_list)
        for j in all_alpha_list:
            if j not in factor_list:
                drop_list.append(j)
            if j in factor_list:
                retain_alpha_list.append(j)
        # print(drop_list)
        # print(retain_alpha_list)
        self.df['overrall_score'] = 0
        for k in retain_alpha_list:
            self.df['overrall_score']=self.df['overrall_score']+self.df[k]
        self.df.drop(drop_list,axis=1,inplace=True)
        self.df[f"overrall_score排名"] = self.df.groupby('日期')['overrall_score'].rank(method='first')
        self.df = self.df[self.df["overrall_score排名"]<=self.select_stock_num]
        self.df.set_index('代码', drop=False, inplace=True)
        self.df.dropna(subset=retain_alpha_list,how='any', inplace=True)
        self.df.drop(['overrall_score排名','overrall_score'],axis=1,inplace=True)

    def get_ic_and_ir(self, factor_list):
        all_alpha_list = self.df.columns.values.tolist()[self.df.columns.values.tolist().index('alpha001'):self.df.columns.values.tolist().index('交易天数')]
        drop_list=[]
        retain_alpha_list=[]
        # print(all_alpha_list)
        # print(factor_list)
        for j in all_alpha_list:
            if j not in factor_list:
                drop_list.append(j)
            if j in factor_list:
                retain_alpha_list.append(j)
        self.df.dropna(subset = retain_alpha_list, how='any', inplace=True)




        for i in factor_list:
            self.df[f'{i}的排名比_icir'] = self.df.groupby('日期')[i].rank(pct=True,method='first')
            self.df[f'{i}的排名_icir'] = self.df.groupby('日期')[i].rank(method='first')
            if self.df['下日_开盘买入涨跌幅'].dtypes!=list:
                self.df['下日_开盘买入涨跌幅'] = self.df['下日_开盘买入涨跌幅'].apply(lambda x: [x])
            self.df['下周期每天涨跌幅'] = self.df['下周期每天涨跌幅'].apply(lambda x: x[1:])
            self.df['下周期每天涨跌幅'] = self.df['下日_开盘买入涨跌幅'] + self.df['下周期每天涨跌幅']

            self.df['下周期涨跌幅'] = self.df['下周期每天涨跌幅'].apply(lambda x: np.cumprod(np.array(list(x))+1)[-1]-1)
            self.df['下周期涨跌幅排名']=self.df.groupby('日期')['下周期涨跌幅'].rank(method='first')
            # print(self.df)
            # exit()
            ic1 = []
            for (k1, k2) in self.df.groupby('日期')['下周期涨跌幅排名', f'{i}的排名_icir']:
                ic = np.corrcoef(k2['下周期涨跌幅排名'], k2[f'{i}的排名_icir'])[0, 1]
                if np.isnan(ic):
                    pass
                else:
                    ic1.append(ic)
            print(np.std(ic1))
            ir = np.average(ic1)/np.std(ic1)
            ic = np.round(np.average(ic1), 5)
            ir = np.round(ir, 5)
            if ir>0:
                self.df[f'{i}的排名_icir'] = self.df.groupby('日期')[f'{i}'].rank(ascending=False, method='first')
            self.ic_ir_dic[f'{i}_ic'] = ic
            self.ic_ir_dic[f'{i}_ir'] = ir
        for key, value in self.ic_ir_dic.items():
            print(f'{key}={value}')

        for i in factor_list:
            self.df.drop([f'{i}的排名比_icir', f'{i}的排名_icir'], axis=1, inplace=True)



    def select_multistock_ic_ir(self,factor_list,icorir):
        all_alpha_list=self.df.columns.values.tolist()[self.df.columns.values.tolist().index('alpha001'):self.df.columns.values.tolist().index('交易天数')]
        drop_list=[]
        retain_alpha_list=[]
        # print(all_alpha_list)
        # print(factor_list)
        for j in all_alpha_list:
            if j not in factor_list:
                drop_list.append(j)
            if j in factor_list:
                retain_alpha_list.append(j)
        # print(drop_list)
        # print(retain_alpha_list)
        self.df.drop(drop_list, axis=1,inplace=True)
        self.df['overrall_score'] = 0

        if icorir=='ic':
            for k in retain_alpha_list:
                self.df['overrall_score']=self.df['overrall_score'] + self.df[k] * self.ic_ir_dic[f'{k}_ic']
        if icorir=='ir':
            for k in retain_alpha_list:
                self.df['overrall_score']=self.df['overrall_score'] + self.df[k] * self.ic_ir_dic[f'{k}_ir']
        self.df['overrall_score排名'] = self.df.groupby('日期')['overrall_score'].rank(method='first')
        self.df = self.df[self.df['overrall_score排名'] <= self.select_stock_num]
        self.df.set_index('代码', drop=False, inplace=True)

        self.df.dropna(subset=retain_alpha_list, how='any', inplace=True)

        # self.df.drop(['overrall_score排名','overrall_score'],axis=1,inplace=True)



# Data=ImportData(file_path,key)
# Data.reset_select_stock_num(3)
# factor_list=['alpha001','alpha002']
# # Data.select_multistock_normal(factor_list)
# Data.get_ic_and_ir(factor_list)
# multistock_type='ic'
# Data.select_multistock_ic_ir(factor_list,multistock_type)
#
# df=Data.df
# df.to_hdf('/Users/zhenghuihuang/Desktop/研究生课程/面向金融的python/大作业/xbx_stock_2019_完整代码/data/adjusted_data_all_3.h5', 'df', format = 'fixed', mode='w')
# print(Data.df)
# exit()
